//
//  MateMapViewController.m
//  TQY
//
//  Created by 甘世清 on 2024/9/30.
//

#import "MateMapViewController.h"

@interface MateMapViewController ()<MKMapViewDelegate>
@property (nonatomic , strong) MKMapView * mapView;
@property (nonatomic , strong) UILabel * addressLabel;
@property (nonatomic , strong) HeaderView * addButton;
@property (nonatomic , strong) NSMutableDictionary * seletedDic;

@end

@implementation MateMapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.zx_navTitle = SQNSLocalString(@"活动地址选择");
    self.zx_navBarBackgroundColor = UIColor.whiteColor;
}
- (void)SQ_UIViewForSelfView{
    WS(weakSelf);
    [self.view addSubview:self.mapView];
    UIImageView * image = [[UIImageView alloc]init];
    image.image = SQGetImage(@"datouzhen");
    image.contentMode = UIViewContentModeScaleAspectFit;
    [self.view addSubview:image];
    [image mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.equalTo(self.view);
        make.width.mas_equalTo(30);
        make.height.mas_equalTo(40);
    }];
    [self.view addSubview:self.addressLabel];
    [self.addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(image.mas_top).offset(-4);
        make.width.mas_equalTo(SCREEN_WIDETH/2.f);
    }];
    
    [self.view addSubview:self.addButton];
    [self.addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.height.mas_equalTo(50);
        make.bottom.equalTo(self.view.mas_bottom).offset(-FootHeight-20);
        make.width.mas_equalTo(SCREEN_WIDETH - 50);
    }];
    
}

//位置已经改变了
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
  
    CLLocationCoordinate2D  location =  [self.mapView convertPoint:CGPointMake(SCREEN_WIDETH/2.f, SCREEN_HEIGHT/2.f) toCoordinateFromView:self.mapView];
         CLGeocoder *gecoder = [[CLGeocoder alloc]init];
         CLLocation *locations = [[CLLocation alloc]initWithLatitude:location.latitude longitude:location.longitude];
    self.seletedDic = [[NSMutableDictionary alloc]init];
    [self.seletedDic setObject:[NSString stringWithFormat:@"%.7f",location.latitude] forKey:@"lat"];
    [self.seletedDic setObject:[NSString stringWithFormat:@"%.7f",location.longitude] forKey:@"lng"];
    WS(weakSelf);
        [gecoder reverseGeocodeLocation:locations completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
            if (placemarks.count > 0) {
                CLPlacemark * place = placemarks.firstObject;
                if ([place.thoroughfare isNotEmpty]) {
                    weakSelf.addressLabel.text = [NSString stringWithFormat:@"%@%@",place.subLocality,place.thoroughfare];
                } else {
                    weakSelf.addressLabel.text = [NSString stringWithFormat:@"%@%@",place.subLocality,place.name];
                }
                weakSelf.addressLabel.text = [weakSelf.addressLabel.text stringByReplacingOccurrencesOfString:@"null" withString:@""];
                weakSelf.addressLabel.text = [weakSelf.addressLabel.text stringByReplacingOccurrencesOfString:@"()" withString:@""];
                weakSelf.addressLabel.text = [weakSelf.addressLabel.text stringByReplacingOccurrencesOfString:@"nil" withString:@""];
            }
        }];
}
-(void)SQ_addAnntionForMap{
    if (self.seletedDic == nil) {
        return;
    }
    if (self.addressBlock) {
        self.addressBlock(self.seletedDic,self.addressLabel.text);
    }
    [self SQ_popViewVC];
}

/** 懒加载地图视图 */
- (MKMapView *)mapView{
    if (!_mapView) {
        _mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, SCREEN_HEIGHT)];
        /** 设置地图类型 */
        _mapView.mapType = MKMapTypeMutedStandard;
        _mapView.userTrackingMode = MKUserTrackingModeFollow;
        //设置地图可缩放
        _mapView.zoomEnabled = YES;
        //设置地图可滚动
        _mapView.scrollEnabled = YES;
        //设置地图可旋转
        _mapView.rotateEnabled = YES;
        //设置显示用户显示位置
        _mapView.showsUserLocation = YES;
        //为MKMapView设置delegate
        _mapView.delegate = self;
        _mapView.showsBuildings = YES;
        _mapView.showsTraffic = YES;
    }
    return _mapView;
}
- (UILabel *)addressLabel{
    if (!_addressLabel) {
        _addressLabel = [[UILabel alloc]init];
         _addressLabel.font = FONT_REGULAR(12);
        _addressLabel.textColor = UIColor.whiteColor;
        _addressLabel.numberOfLines = 0;
        _addressLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _addressLabel;
}

- (HeaderView *)addButton{
    if (!_addButton) {
        _addButton = [[HeaderView alloc]init];
        _addButton.backgroundColor = SQColor3DA59D;
        _addButton.colorString = @"#FFFFFF";
        [_addButton.btn addTarget:self action:@selector(SQ_addAnntionForMap) forControlEvents:UIControlEventTouchUpInside];
        _addButton.layer.cornerRadius = 25;
        _addButton.layer.masksToBounds = YES;
        _addButton.text = SQNSLocalString(@"确认");
        _addButton.font = FONT_MEDIUM(16);
    }
    return _addButton;
}
@end
